<?php
namespace User\Controller;
use Think\Page;
use User\Model\FactoryModel;
use User\Model\LiveModel;

class LiveController extends UserController
{
    public function __construct()
    {
        parent::__construct();
        if (!headers_sent() && // 如果页面头部信息还没有输出
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    public function index()
    {
        $live_room = M('live_room');
        $live_user = M('live_user');
        $where['status'] = array('NEQ', LiveModel::ROOM_DELETE);
        $where['reserve'] = $this->wxuser['token'];
        $title = I("get.title", "", "");
        if (!empty($title)) {
            $where['title'] = array('LIKE', "%" . $title . "%");
            $this->assign("title", $title);
        }
        $count = $live_room->where($where)->count();
        $page = new Page($count, 10);
        $show = $page->show();
        $liveUser = array();
        $list = $live_room->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')
            ->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $userids[] = $value['userid'];
                $value['link'] = "http://" . $_SERVER['SERVER_NAME'] . "/Wap/Live/index?live_id=" . $value['id'];
            }
            if (!empty($userids)) {
                $users_where['id'] = array('IN', $userids);
                $users = $live_user->where($users_where)->select();
                if (!empty($users)) {
                    foreach ($users as &$user) {
                        $liveUser[$user['id']]['account'] = $user['account'];
                        $liveUser[$user['id']]['createdate'] = date("Y-m-d", strtotime($user['createdate']));
                    }
                }
            }
        }
        $this->assign("page", $show);
        $this->assign("list", $list);
        $this->assign("liveUser", $liveUser);
        $this->display();
    }

    public function edit()
    {
        $id = I("get.id", -1, "");
        $live_room = $live_user = array();
        if ($id != -1) {
            $condition['id'] = $id;
            $condition['status'] = array('NEQ', LiveModel::ROOM_DELETE);
            $live_room = M("live_room")->where($condition)->find();
            $live_user = M("live_user")->where(['id' => $live_room['userid'], 'status' => LiveModel::USER_ACTIVE])
                ->find();
        }
        $live_category = M("live_category")->where(['parentid' => 0, 'status' => LiveModel::CATEGORY_ACTIVE])
            ->order('displayorder desc')->select();
        $this->assign("id", $id);
        $this->assign("live_room", $live_room);
        $this->assign("live_user", $live_user);
        $this->assign("live_category", $live_category);
        $this->display();
    }

    public function update()
    {
        $id = I("post.id", -1, "");
        $data_user = array(
            'account' => I("post.account", "", "") ? I("post.account", "", "") : I("post.new_account", "", ""),
            'password' => I("post.new_password", "", "") ? md5(I("post.new_password", "", "")) : I("post.old_password", "", ""),
            'hasroom' => LiveModel::HASROOM_YES,
            'avatar' => I("post.avatar", "", "")
        );
        if ($id == -1) {
            $find_account = M("live_user")->where(['account' => $data_user['account'],
                'status' => LiveModel::USER_ACTIVE])->find();
            if (!empty($find_account)) {
                $this->error("主播账号已被注册过，请重新注册主播账号", "/User/Live/index");
            }
        }
        $data_room = array(
            'title' => I("post.title", "", ""),
            'category' => I("post.category", "", ""),
            'share_title' => I("post.share_title", "", ""),
            'share_desc' => I("post.share_desc", "", ""),
            'share_link' => I("post.share_link", "", ""),
            'poster' => I("post.poster", "", "") ? I("post.poster", "", "") : '/Public/images/live/home_bg.png',
            'share_image' => I("post.share_image", "", ""),
            'reserve' => $this->wxuser['token'],
            'info' => I("post.info", "", "")
        );
        $live_room = M("live_room");
        $live_user = M("live_user");
        if ($id != -1 && !empty($id)) {
            $roomid = $data_room['id'] = $data_user['roomid'] = $id;
            $live_room->save($data_room);
            $live_room_info = $live_room->where(['id' => $roomid])->find();
            $userid = $live_room_info['userid'];
            $live_user->where(['id' => $userid])->save($data_user);
        } else {
            $userid = $data_room['userid'] = $live_user->add($data_user);
            $roomid = $live_room->add($data_room);
            $live_user->where(['id' => $userid])->save(array("roomid" => $roomid));
        }
        if ($userid > 0 && $roomid > 0) {
            $this->success("保存成功", "/User/Live/index");
        } else {
            $this->error("保存失败", "/User/Live/index");
        }
    }

    public function deletes()
    {
        $ids = I("get.ids", "", "");
        if (!empty($ids)) {
            $ids = explode('|', $ids);
        }
        $condition['id'] = array('IN', $ids);
        $updated = M("live_room")->where($condition)->save(array('status' => LiveModel::ROOM_DELETE));
        if ($updated) {
            $this->success("删除成功", "", true);
        } else {
            $this->error("删除失败", "", true);
        }
    }

    public function statusLive()
    {
        $status = I("get.status", "", "");
        $condition['id'] = I("get.id", "", "");
        if ($status == "active") {
            M("live_room")->where($condition)->save(array("status" => LiveModel::ROOM_ACTIVE));
            $message = "发布成功";
        } elseif ($status == "apply") {
            M("live_room")->where($condition)->save(array("status" => LiveModel::ROOM_APPLY));
            $message = "停用成功";
        } else {
            M("live_room")->where($condition)->save(array("status" => LiveModel::ROOM_DELETE));
            $message = "删除成功";
        }
        $this->success($message, "", true);
    }

    public function uploadFile()
    {
        $img = I("post.img", "", "");
        $ret = FactoryModel::ajaxImgUpload($img, 'live');
        echo json_encode($ret);
        exit;
    }

    public function category()
    {
        $where['status'] = array("NEQ", LiveModel::CATEGORY_DELETE);
        $where['reserve'] = $this->wxuser['token'];
        $list = M("live_category")->where($where)->order('displayorder desc')->select();
        $this->assign("list", $list);
        $this->display();
    }

    public function editCategory()
    {
        $id = I("get.id", -1, "");
        $live_category = array();
        if ($id != -1) {
            $condition['id'] = $id;
            $condition['status'] = array("NEQ", LiveModel::CATEGORY_DELETE);
            $live_category = M("live_category")->where($condition)->find();
        }
        $this->assign("id", $id);
        $this->assign("live_category", $live_category);
        $this->display();
    }

    public function updateCategory()
    {
        $id = I("post.id", -1, "");
        $data_category = array(
            'name' => I("post.name", "", ""),
            'displayorder' => I("post.displayorder", 0, ""),
            'status' => I("post.status", LiveModel::CATEGORY_INACTIVE, ""),
            'reserve' => $this->wxuser['token']
        );
        $live_category = M("live_category");
        if ($id != -1 && !empty($id)) {
            $data_category['id'] = $id;
            $updated = $live_category->save($data_category);
        } else {
            $updated = $live_category->add($data_category);
        }
        if ($updated > 0 || ($updated == 0 && $id > 0)) {
            $this->success("保存成功", "/User/Live/category");
        } else {
            $this->error("保存成功", "/User/Live/category");
        }
    }

    public function deleteCategory()
    {
        $id = I("get.id", "", "");
        $updated = M("live_category")->where(["id" => $id])->save(array('status' => LiveModel::CATEGORY_DELETE));
        if ($updated > 0) {
            $this->success("删除成功", "", true);
        } else {
            $this->error("删除失败", "", true);
        }
    }

    public function topic()
    {
        $title = I("get.title", "", "");
        $room_id = I("get.room_id", -1, "");
        $where['status'] = array('NEQ', LiveModel::TOPIC_DELETE);
        $where['token'] = $this->wxuser['token'];
        $where['room_id'] = $room_id;
        if (!empty($title)) {
            $where['topic_title'] = array('LIKE', "%" . $title . "%");
            $this->assign("title", $title);
        }
        $live_topic = M('live_topic');
        $count = $live_topic->where($where)->count();
        $page = new Page($count, 15);
        $show = $page->show();
        $list = $live_topic->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('id desc')
            ->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $link_video = "视频直播：http://" . $_SERVER['SERVER_NAME'] . "/Wap/Live/index?to=topic&live_id=" . $room_id .
                    "&topic_id=" . $value['id'];
                $link_msg = "图文直播：http://" . $_SERVER['SERVER_NAME'] . "/Wap/Live/index?to=room_img&live_id=" . $room_id .
                    "&topic_id=" . $value['id'];
                if ($value["topic_type"] != LiveModel::TOPIC_TYPE_VIDEO) {
                    $value["link"] = $link_video . "<br>" . $link_msg;
                } else {
                    $value["link"] = $link_video;
                }
                $value['admin_link'] = "http://" . $_SERVER['SERVER_NAME'] . "/Wap/Live/index?to=assistant&live_id=" .
                    $room_id . "&topic_id=" . $value['id'];
                $reserve = json_decode($value['reserve'], true);
                $value['replay'] = $reserve['replay'] ? $reserve['replay'] : 9;
                $value['replay_address'] = $reserve['replay_address'] ? $reserve['replay_address'] : '';
                $value['rec_replay'] = $reserve['rec_replay'] ? $reserve['rec_replay'] : 9;
                $value['rec_replay_id'] = $reserve['rec_replay_id'] ? explode(',', $reserve['rec_replay_id']) : '';
            }
        }
        //回放列表
        $where_replay['status'] = array('NEQ', LiveModel::TOPIC_DELETE);
        $where_replay['reserve'] = array('LIKE', '%"replay":"1"%');
        $replay_list = $live_topic->where($where_replay)->order('id asc')->select();
        $this->assign("room_id", $room_id);
        $this->assign("page", $show);
        $this->assign("list", $list);
        $this->assign("replay_list", $replay_list);
        $this->display();
    }

    public function replay()
    {
        $replay = I("get.replay", LiveModel::REPLAY_FALSE, "");
        $replay_address = I("get.replay_address", "", "");
        $topic_id = I("get.topic_id", -1, "");
        $live_topic = M('live_topic')->where(['id' => $topic_id])->find();
        $reserve = json_decode($live_topic['reserve'], true);

        $data = array(
            'banner' => $reserve['banner'] ? $reserve['banner'] : '',
            'replay' => $replay,
            'replay_address' => ($replay == LiveModel::REPLAY_TRUE) ? $replay_address : '',
            'rec_replay' => $reserve['rec_replay'] ? $reserve['rec_replay'] : '',
            'rec_replay_id' => $reserve['rec_replay_id'] ? $reserve['rec_replay_id'] : ''
        );
        M("live_topic")->where(['id' => $topic_id])->save(array('reserve' => json_encode($data)));
        $message = "回放设置成功";
        $this->success($message, "", true);
    }

    public function recReplay()
    {
        $rec_replay = I("get.rec_replay", LiveModel::REPLAY_FALSE, "");
        $rec_replay_id = I("get.rec_replay_id", "", "");
        $topic_id = I("get.topic_id", -1, "");
        $live_topic = M('live_topic')->where(['id' => $topic_id])->find();
        $reserve = json_decode($live_topic['reserve'], true);

        $data = array(
            'banner' => $reserve['banner'] ? $reserve['banner'] : '',
            'replay' => $reserve['replay'] ? $reserve['replay'] : '',
            'replay_address' => $reserve['replay_address'] ? $reserve['replay_address'] : '',
            'rec_replay' => $rec_replay,
            'rec_replay_id' => ($rec_replay == LiveModel::REPLAY_TRUE) ? $rec_replay_id : ''
        );
        M("live_topic")->where(['id' => $topic_id])->save(array('reserve' => json_encode($data)));
        $message = "推荐回放设置成功";
        $this->success($message, "", true);
    }

    public function editTopic()
    {
        $id = I("get.id", -1, "");
        $room_id = I("get.room_id", -1, "");
        $live_topic = array();
        if ($room_id != -1) {
            $condition['id'] = $id;
            $condition['token'] = $this->wxuser['token'];
            $condition['status'] = array('NEQ', LiveModel::TOPIC_DELETE);
            $live_topic = M("live_topic")->where($condition)->find();
            $support = json_decode($live_topic['support'], true);
            $live_topic['support_comment'] = $support['support_comment'];
            $live_topic['support_follow'] = $support['support_follow'];
            $live_topic['follow_delay_time'] = $support['follow_delay_time'];
            $live_topic['support_online_num'] = $support['support_online_num'];
            $live_topic['online_multi'] = $support['online_multi'];
            $live_topic['support_praise'] = $support['support_praise'];
            $live_topic['support_comment_check'] = $support['support_comment_check'];

            $reserve = json_decode($live_topic['reserve'], true);
            $live_topic['banner'] = $reserve['banner'] ? $reserve['banner'] : '';
        }
        $this->assign("id", $id);
        $this->assign("room_id", $room_id);
        $this->assign("live_topic", $live_topic);
        $this->display();
    }

    public function updateTopic()
    {
        $id = I("post.id", -1, "");
        $room_id = I("post.room_id", 0, "");
        $data_topic = array(
            'token' => $this->wxuser['token'],
            'room_id' => $room_id,
            'topic_title' => I("post.topic_title", "", ""),
            'topic_info' => I("post.topic_info", "", ""),
            'startdate' => I("post.startdate", "", ""),
            'enddate' => I("post.enddate", "", ""),
            'topic_type' => I("post.topic_type", LiveModel::TOPIC_TYPE_VIDEO, ""),
            'assist_pwd' => I("post.assist_pwd", "", ""),
            'share_title' => I("post.share_title", "", ""),
            'share_desc' => I("post.share_desc", "", ""),
            'share_link' => I("post.share_link", "", ""),
            'share_image' => I("post.share_image", "", "")
        );
        if ($data_topic['topic_type'] == LiveModel::TOPIC_TYPE_VIDEO) {
            $data_topic['video_poster'] = I("post.video_poster", "", "");
            $data_topic['ad_link'] = I("post.ad_link", "", "");
            $data_topic['ad_image'] = I("post.ad_image", "", "");
            $data_topic['input_stream'] = I("post.input_stream", "", "");
            $data_topic['output_stream'] = I("post.output_stream", "", "");
            $data_topic['msg_poster'] = "";
            $data_topic['sort'] = LiveModel::SORT_ASC;
            $banner = '';
        } elseif ($data_topic['topic_type'] == LiveModel::TOPIC_TYPE_MSG) {
            $data_topic['video_poster'] = "";
            $data_topic['ad_link'] = "";
            $data_topic['ad_image'] = "";
            $data_topic['input_stream'] = "";
            $data_topic['output_stream'] = "";
            $data_topic['msg_poster'] = I("post.msg_poster", "", "");
            $data_topic['sort'] = I("post.sort", LiveModel::SORT_ASC, "");
            $banner = I("post.banner", "", "");
        } else {
            $data_topic['video_poster'] = I("post.video_poster", "", "");
            $data_topic['ad_link'] = I("post.ad_link", "", "");
            $data_topic['ad_image'] = I("post.ad_image", "", "");
            $data_topic['input_stream'] = I("post.input_stream", "", "");
            $data_topic['output_stream'] = I("post.output_stream", "", "");
            $data_topic['msg_poster'] = I("post.msg_poster", "", "");
            $data_topic['sort'] = I("post.sort", LiveModel::SORT_ASC, "");
            $banner = I("post.banner", "", "");
        }
        if ($data_topic['topic_type'] == LiveModel::TOPIC_TYPE_MSG) {
            $support = array(
                'support_comment' => (I("post.support_comment", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE,
                'support_follow' => LiveModel::SUPPORT_DELETE,
                'follow_delay_time' => "",
                'support_online_num' => LiveModel::SUPPORT_DELETE,
                'online_multi' => 0,
                'support_praise' => LiveModel::SUPPORT_DELETE,
                'support_comment_check' => (I("post.support_comment_check", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE
            );
        } else {
            $support = array(
                'support_comment' => (I("post.support_comment", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE,
                'support_follow' => (I("post.support_follow", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE,
                'follow_delay_time' => (I("post.support_follow", "off", "") == "on") ? I("post.follow_delay_time", 10, "") : "",
                'support_online_num' => (I("post.support_online_num", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE,
                'online_multi' => (I("post.support_online_num", "off", "") == "on") ? I("post.online_multi", 0, "") : "",
                'support_praise' => (I("post.support_praise", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE,
                'support_comment_check' => (I("post.support_comment_check", "off", "") == "on") ? LiveModel::SUPPORT_ACTIVE :
                    LiveModel::SUPPORT_DELETE
            );
        }
        $data_topic['support'] = json_encode($support, JSON_UNESCAPED_UNICODE);
        $live_topic = M("live_topic");
        if ($id != -1 && !empty($id)) {
            $data_topic['id'] = $id;
            $reserve = M('live_topic')->where(['id' => $id])->getField('reserve');
            $reserve = json_decode($reserve, true);
            $data_topic['reserve'] = json_encode(array('banner' => $banner, 'replay' => $reserve['replay'],
                'replay_address' => $reserve['replay_address'], 'rec_replay' => $reserve['rec_replay'],
                'rec_replay_id' => $reserve['rec_replay_id']), JSON_UNESCAPED_UNICODE);
            $updated = $live_topic->save($data_topic);
        } else {
            $data_topic['reserve'] = json_encode(array('banner' => $banner, 'replay' => LiveModel::REPLAY_FALSE,
                'replay_address' => '', 'rec_replay' => LiveModel::REPLAY_FALSE, 'rec_replay_id' => ''), JSON_UNESCAPED_UNICODE);
            $updated = $live_topic->add($data_topic);
        }
        if ($updated > 0 || ($id > 0 && $updated == 0)) {
            $this->success("保存成功", "/User/Live/topic?room_id=" . $room_id);
        } else {
            $this->error("保存失败", "/User/Live/topic?room_id=" . $room_id);
        }
    }

    public function deleteTopic()
    {
        $ids = I("get.ids", "", "");
        if (!empty($ids)) {
            $ids = explode('|', $ids);
        }
        $condition['id'] = array('IN', $ids);
        $updated = M("live_topic")->where($condition)->save(array('status' => LiveModel::TOPIC_DELETE));
        if ($updated) {
            $this->success("删除成功", "", true);
        } else {
            $this->error("删除失败", "", true);
        }
    }

    public function statusTopic()
    {
        $status = I("get.status", "", "");
        $condition['id'] = I("get.id", "", "");
        if ($status == "active") {
            M("live_topic")->where($condition)->save(array("status" => LiveModel::TOPIC_ACTIVE));
            $message = "发布成功";
        } elseif ($status == "apply") {
            M("live_topic")->where($condition)->save(array("status" => LiveModel::TOPIC_APPLY));
            $message = "停用成功";
        } else {
            M("live_topic")->where($condition)->save(array("status" => LiveModel::TOPIC_DELETE));
            $message = "删除成功";
        }
        $this->success($message, "", true);
    }

    public function blacklist()
    {
        $nickname = I("get.nickname", "", "");
        $topic_id = I("get.topic_id", -1, "");
        $where['status'] = array('EQ', LiveModel::MEMBER_SILENT);
        $where['topic_id'] = $topic_id;
        if (!empty($nickname)) {
            $where['reserve'] = array('LIKE', "%" . $nickname . "%");
            $this->assign("nickname", $nickname);
        }
        $count = M('live_member')->where($where)->count('distinct openid');
        $page = new Page($count, 15);
        $show = $page->show();
        $list = M('live_member')->where($where)->group('openid')->field('id,openid,createdate,status,reserve')
            ->limit($page->firstRow . ',' . $page->listRows)->order('createdate desc')->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['nickname'] = json_decode($value['reserve']);
            }
        }
        $this->assign("topic_id", $topic_id);
        $this->assign("page", $show);
        $this->assign("list", $list);
        $this->display();
    }

    public function deleteMember()
    {
        $topic_id = I("get.topic_id", -1, "");
        $openids = I("get.openids", "", "");
        if (!empty($openids)) {
            $openids = explode('|', $openids);
        }
        $condition['topic_id'] = $topic_id;
        $condition['openid'] = array('IN', $openids);
        $updated = M("live_member")->where($condition)->save(array('status' => LiveModel::MEMBER_DELETE));
        if ($updated) {
            $this->success("删除成功", "", true);
        } else {
            $this->error("删除失败", "", true);
        }
    }

    public function activateMember()
    {
        $topic_id = I("get.topic_id", -1, "");
        $openids = I("get.openids", "", "");
        if (!empty($openids)) {
            $openids = explode('|', $openids);
        }
        $condition['topic_id'] = $topic_id;
        $condition['openid'] = array('IN', $openids);
        $updated = M("live_member")->where($condition)->save(array('status' => LiveModel::MEMBER_ACTIVE));
        if ($updated) {
            $this->success("还原成功", "", true);
        } else {
            $this->error("还原失败", "", true);
        }
    }

    public function statusMember()
    {
        $status = I("get.status", "", "");
        $condition['topic_id'] = I("get.topic_id", -1, "");
        $condition['openid'] = I("get.openid", "", "");
        if ($status == "active") {
            M("live_member")->where($condition)->save(array("status" => LiveModel::MEMBER_ACTIVE));
            $message = "还原成功";
        } else {
            M("live_member")->where($condition)->save(array("status" => LiveModel::MEMBER_DELETE));
            $message = "删除成功";
        }
        $this->success($message, "", true);
    }

    public function comment()
    {
        $content = I("get.content", "", "");
        $topic_id = I("get.topic_id", -1, "");
        $support = M("live_topic")->where(['id' => $topic_id])->getField("support");
        $support = json_decode($support, true);
        $support_comment = $support['support_comment'];
        $support_comment_check = $support['support_comment_check'];
        $where['token'] = $this->wxuser['token'];
        $where['topic_id'] = $topic_id;
        $where['comment_type'] = array('EQ', LiveModel::USER_VIEWER);
        $where['msg_type'] = LiveModel::MSG_TEXT;
        $where['status'] = array('neq', LiveModel::COMMENT_DELETE);
        if (!empty($content)) {
            $where['content'] = array('LIKE', "%" . $content . "%");
            $this->assign("content", $content);
        }
        $live_comment = M('live_comment');
        $count = $live_comment->where($where)->count();
        $page = new Page($count, 15);
        $show = $page->show();
        $member = $liveMember = $live_member = array();
        $list = $live_comment->where($where)->limit($page->firstRow . ',' . $page->listRows)
            ->order('createdate desc')->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['content'] = json_decode($value['content']);
            }
            foreach ($list as $value) {
                $member[] = $value['openid'];
            }
            $member = array_flip(array_flip($member));
            $liveMember = M("live_member")->where(['topic_id' => $topic_id, 'openid' => array('in', $member)])
                ->field("openid,reserve")->select();
            foreach ($liveMember as $value) {
                $live_member[$value['openid']] = json_decode($value['reserve']);
            }
        }
        $this->assign("support_comment", $support_comment);
        $this->assign("support_comment_check", $support_comment_check);
        $this->assign("topic_id", $topic_id);
        $this->assign("page", $show);
        $this->assign("list", $list);
        $this->assign("live_member", $live_member);
        $this->display();
    }

    public function deleteComment()
    {
        $ids = I("get.ids", "", "");
        if (!empty($ids)) {
            $ids = explode('|', $ids);
        }
        $condition['id'] = array('IN', $ids);
        $updated = M("live_comment")->where($condition)->save(array('status' => LiveModel::COMMENT_DELETE));
        if ($updated) {
            $this->success("删除成功", "", true);
        } else {
            $this->error("删除失败", "", true);
        }
    }

    public function activateComment()
    {
        $ids = I("get.ids", "", "");
        if (!empty($ids)) {
            $ids = explode('|', $ids);
        }
        $condition['id'] = array('IN', $ids);
        $updated = M("live_comment")->where($condition)->save(array('status' => LiveModel::COMMENT_ACTIVE));
        if ($updated) {
            $this->success("还原成功", "", true);
        } else {
            $this->error("还原失败", "", true);
        }
    }

    public function passComment()
    {
        $ids = I("get.ids", "", "");
        if (!empty($ids)) {
            $ids = explode('|', $ids);
        }
        $condition['id'] = array('IN', $ids);
        $updated = M("live_comment")->where($condition)->save(array('status' => LiveModel::COMMENT_ACTIVE));
        if ($updated) {
            $this->success("通过成功", "", true);
        } else {
            $this->error("通过失败", "", true);
        }
    }

    public function statusComment()
    {
        $status = I("get.status", "", "");
        $condition['id'] = I("get.id", "", "");
        if ($status == "active") {
            M("live_comment")->where($condition)->save(array("status" => LiveModel::COMMENT_ACTIVE));
            $message = "还原成功";
        } elseif ($status == "pass") {
            M("live_comment")->where($condition)->save(array("status" => LiveModel::COMMENT_ACTIVE));
            $message = "通过成功";
        } else {
            M("live_comment")->where($condition)->save(array("status" => LiveModel::COMMENT_DELETE));
            $message = "删除成功";
        }
        $this->success($message, "", true);
    }

    //动态切换是否开启评论
    public function supportComment()
    {
        $topic_id = I("get.topic_id", -1, "");
        $where["id"] = $topic_id;
        $support = M("live_topic")->where($where)->getField("support");
        $support = json_decode($support, true);
        $support_comment = $support['support_comment'];
        $data = array(
            "support_follow" => $support["support_follow"],
            "follow_delay_time" => $support["follow_delay_time"],
            "support_online_num" => $support["support_online_num"],
            "online_multi" => $support["online_multi"],
            "support_praise" => $support["support_praise"],
            "support_comment_check" => $support["support_comment_check"]
        );
        if ($support_comment == LiveModel::SUPPORT_ACTIVE) {
            $data["support_comment"] = LiveModel::SUPPORT_DELETE;
            $message = "关闭评论成功";
        } else {
            $data["support_comment"] = LiveModel::SUPPORT_ACTIVE;
            $message = "开启评论成功";
        }
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        M("live_topic")->where($where)->save(array("support" => $data));
        $this->success($message, "", true);
    }

    public function statistics()
    {
        $topic_id = I("get.topic_id", -1, "");
        $live_topic = M("live_topic")->where(["id" => $topic_id, "status" =>
            array('in', LiveModel::TOPIC_ACTIVE . ',' . LiveModel::TOPIC_PAUSE)])->field("startdate,enddate")->find();
        $where['topic_id'] = $topic_id;
        $where['status'] = LiveModel::STAT_ACTIVE;
        $startdate = strtotime($live_topic['startdate']) - 3600;
        $where['createdate'] = array("EGT", date("Y-m-d H:i:s", $startdate));
        $pv = M("live_statistics")->where($where)->sum("page_view");
        $comment_num = M("live_statistics")->where($where)->sum("comment_num");
        $praise_num = M("live_statistics")->where($where)->sum("praise_num");
        $cache = S("live_topic_" . $topic_id);
        if ($cache) {
            $pv = $cache['page_view'] + $pv;
            $comment_num = $cache['comment_num'] + $comment_num;
            $praise_num = $cache['praise_num'] + $praise_num;
        }
        $uv = M("live_member")->where(["topic_id" => $topic_id, "status" => LiveModel::MEMBER_ACTIVE])->count();
        $where_list["topic_id"] = $topic_id;
        $where_list['createdate'] = array("EGT", date("Y-m-d H:i:s", $startdate));
        $where_list["status"] = LiveModel::STAT_ACTIVE;
        $list = M("live_statistics")->where($where_list)->order("createdate asc")
            ->field("play_time,page_view,praise_num,comment_num")->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['play_time'] = $value['play_time'] . 'h';
            }
            $cached = true;
        } else {
            $cached = false;
        }
        $list = json_encode($list);
        $this->assign("pv", $pv ? $pv : 0);
        $this->assign("uv", $uv);
        $this->assign("comment_num", $comment_num ? $comment_num : 0);
        $this->assign("praise_num", $praise_num ? $praise_num : 0);
        $this->assign("cached", $cached);
        $this->assign("topic_id", $topic_id);
        $this->assign("list", $list);
        $this->display("statistics_graph");
    }

    public function statisticsUser()
    {
        $topic_id = I("get.topic_id", -1, "");
        $where['status'] = array('EQ', LiveModel::MEMBER_ACTIVE);
        $where['topic_id'] = $topic_id;
        $where['user_type'] = LiveModel::USER_VIEWER;
        $live_member = M("live_member");
        $count = $live_member->where($where)->count();
        $page = new Page($count, 15);
        $show = $page->show();
        $list = $live_member->where($where)->limit($page->firstRow . ',' . $page->listRows)->order('createdate desc')
            ->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                if ($value['has_comment'] == LiveModel::HAS_COMMENT_YES) {
                    $value['has_comment'] = '是';
                } else {
                    $value['has_comment'] = '否';
                }
            }
        }
        $this->assign("page", $show);
        $this->assign("topic_id", $topic_id);
        $this->assign("list", $list);
        $this->display("statistics_user");
    }

    public function exportMember()
    {
        set_time_limit(0);
        $topic_id = I("get.topic_id", "", "");
        if (empty($topic_id)) {
            $this->redirect("/User/Live/index");
        }
        vendor('phpexcel.PHPExcel');
        $objExcel = new \PHPExcel();
        $objActSheet = $objExcel->getActiveSheet();
        $objActSheet->getColumnDimension('A')->setWidth(10);
        $objActSheet->getColumnDimension('B')->setWidth(30);
        $objActSheet->getColumnDimension('C')->setWidth(30);
        $objActSheet->getColumnDimension('D')->setWidth(30);
        $objActSheet->getColumnDimension('E')->setWidth(10);
        $objActSheet->getColumnDimension('F')->setWidth(30);
        $objActSheet->setCellValue('A1', "编号");
        $objActSheet->setCellValue('B1', "时间");
        $objActSheet->setCellValue('C1', "openid");
        $objActSheet->setCellValue('D1', "是否评论");
        $objActSheet->setCellValue('E1', "地域");
        $objActSheet->setCellValue('F1', "手机型号");
        $condition['topic_id'] = $topic_id;
        $condition['status'] = LiveModel::MEMBER_ACTIVE;
        $condition['user_type'] = LiveModel::USER_VIEWER;
        $line = 2;
        $list = M("live_member")->where($condition)->order('createdate desc')->select();
        if (!empty($list)) {
            foreach ($list as $value) {
                if ($value['has_comment'] == LiveModel::HAS_COMMENT_YES) {
                    $has_comment = '是';
                } else {
                    $has_comment = '否';
                }
                $objActSheet->setCellValue('A' . $line, $value['id']);
                $objActSheet->setCellValue('B' . $line, $value['createdate']);
                $objActSheet->setCellValue('C' . $line, $value['openid']);
                $objActSheet->setCellValue('D' . $line, $has_comment);
                $objActSheet->setCellValue('E' . $line, $value['area']);
                $objActSheet->setCellValue('F' . $line, $value['phone_type']);
                $objActSheet->getRowDimension($line)->setRowHeight(20);
                $line++;
            }
        }
        $fileName = '用户行为' . date('Ymd') . '.xlsx';
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');
        $objWriter = new \PHPExcel_Writer_Excel2007($objExcel);
        $objWriter->save('php://output');
    }

    public function assist()
    {
        $nickname = I("get.nickname", "", "");
        $topic_id = I("get.topic_id", -1, "");
        $where['status'] = array('EQ', LiveModel::MEMBER_ACTIVE);
        $where['user_type'] = array('EQ', LiveModel::USER_ASSIST);
        $where['topic_id'] = $topic_id;
        if (!empty($nickname)) {
            $where['reserve'] = array('LIKE', "%" . $nickname . "%");
            $this->assign("nickname", $nickname);
        }
        $count = M('live_member')->where($where)->count('distinct openid');
        $page = new Page($count, 15);
        $show = $page->show();
        $list = M('live_member')->where($where)->group('openid')->field('id,openid,createdate,status,reserve')
            ->limit($page->firstRow . ',' . $page->listRows)->order('createdate desc')->select();
        if (!empty($list)) {
            foreach ($list as &$value) {
                $value['nickname'] = json_decode($value['reserve']);
            }
        }
        $this->assign("topic_id", $topic_id);
        $this->assign("page", $show);
        $this->assign("list", $list);
        $this->display();
    }

    public function deleteAssist()
    {
        $topic_id = I("get.topic_id", -1, "");
        $openids = I("get.openids", "", "");
        if (!empty($openids)) {
            $openids = explode('|', $openids);
        }
        $condition['topic_id'] = $topic_id;
        $condition['openid'] = array('IN', $openids);
        $updated = M("live_member")->where($condition)->save(array('user_type' => LiveModel::USER_VIEWER));
        if ($updated) {
            $this->success("删除助理成功", "", true);
        } else {
            $this->error("删除助理失败", "", true);
        }
    }

    public function statusAssist()
    {
        $condition['topic_id'] = I("get.topic_id", -1, "");
        $condition['openid'] = I("get.openid", "", "");
        M("live_member")->where($condition)->save(array("user_type" => LiveModel::USER_VIEWER));
        $message = "删除成功";
        $this->success($message, "", true);
    }
}

?>
